﻿function callClientFunction ( client, funcname, ... )
    local arg = { ... }
    if ( arg [ 1 ] ) then
        for key, value in next, arg do
            if ( type ( value ) == "number" ) then arg [ key ] = tostring ( value ) end
        end
    end
    -- If the clientside event handler is not in the same resource, replace 'resourceRoot' with the appropriate element
    triggerClientEvent ( client, "onServerCallsClientFunction", resourceRoot, funcname, unpack ( arg or {} ) )
end

function callServerFunction ( funcname, ... )
    local arg = { ... }
    if ( arg [ 1 ] ) then
        for key, value in next, arg do arg [ key ] = tonumber ( value ) or value end
    end
    loadstring ( "return " .. funcname ) () ( unpack ( arg ) )
end
addEvent ( "onClientCallsServerFunction", true )
addEventHandler ( "onClientCallsServerFunction", resourceRoot , callServerFunction )

-- format of data
-- creator = { string elementType1, string elementType2, ... }
-- elementType* = { int first ID, int last ID, { exception1, exception2, ... } }
creatorTable = { ped = { 0, 288, { 3, 4, 5, 6, 8, 42, 65, 74, 86, 119, 149, 208, 239, 273 } }, pojazd = { 400, 611, { 570 } }, [ "broń" ] = { 321, 373, { 332, 340, 345, 354, 370 } }, obiekt = { 615, 18630, {} } }
function creatorError ( player )
	outputChatBox ( "Złe #9696ffID/nazwa pojazdu#6464ff podane lub tworzenie się nie powiodło!", player, 100, 100, 255, true )
end
function P3create ( player, vehicle, id )
	if tonumber ( id ) then
		id = tonumber ( id )
		local elementType
		for theType, values in pairs ( creatorTable ) do
			if id >= values [ 1 ] and id <= values [ 2 ] then
				for k, v in ipairs ( values [ 3 ] ) do
					if id == v then
						creatorError ( player )
						return
					end
				end
				elementType = theType
				break
			end
		end
		if elementType then
			local element
			if elementType == "ped" then
				element = createPed ( id, 0, 0, 0 )
			elseif elementType == "pojazd" then
				element = createVehicle ( id, 0, 0, 0 )
			elseif elementType == "broń" or "obiekt" then
				element = createObject ( id, 0, 0, 0 )
			end
			if attachElements ( element, vehicle ) then
				outputChatBox ( "Pomyślnie stworzono #9696ff" .. elementType .. "#6464ff, używając ID #9696ff" .. id .. "#6464ff!", player, 100, 100, 255, true )
				setElementData ( element, "createdByP3", true )
				callClientFunction ( player, "g_GridUpdate" )
				return
			else
				destroyElement ( element )
			end
		end
		creatorError ( player )
	elseif tostring ( id ) then
		local matches = {}
		for i = 400, 611 do
			if i ~= 570 then
				if string.find ( string.lower ( getVehicleNameFromModel ( i ) ), id ) then
					table.insert ( matches, i )
				end
			end
		end
		if #matches == 0 then
			outputChatBox ( "Nazwa pojazdu #9696ff" .. id .. "#6464ff nie pasuje do żadnego pojazdu!", player, 100, 100, 255, true )
		elseif #matches == 1 then
			local model = matches [ 1 ]
			local element = createVehicle ( model, 0, 0, 0 )
			if attachElements ( element, vehicle ) then
				outputChatBox ( "Pomyślnie stworzono #9696ff" .. getVehicleNameFromModel ( model ) .. "#6464ff używając nazwy pojazdu #9696ff" .. id .. "#6464ff!", player, 100, 100, 255, true )
				setElementData ( element, "createdByP3", true )
				callClientFunction ( player, "g_GridUpdate" )
				return
			else
				destroyElement ( element )
			end
		elseif #matches > 1 then
			outputChatBox ( "Nazwa pojazdu #9696ff" .. id .. "#6464ff pasuje do #9696ff" .. #matches .. "#6464ff pojazdów!", player, 100, 100, 255, true )
		end
	else
		creatorError ( player )
	end
end

function P3destroy ( player, element, elementName )
	if destroyElement ( element ) then
		outputChatBox ( "Pomyślnie zniszczono #9696ff" .. elementName .. "#6464ff.", player, 100, 100, 255, true )
		callClientFunction ( player, "g_GridUpdate" )
	else
		outputChatBox ( "Niszczenie #9696ff" .. elementName .. "#6464ff nie powiodło się!", player, 100, 100, 255, true )
	end
end

function P3detach ( player, element, elementName )
	local boolean = getElementCollisionsEnabled ( element )
	if detachElements ( element ) then
		if not boolean then
			setElementCollisionsEnabled ( element, true )
		end
		outputChatBox ( "Pomyślnie odczepiono #9696ff" .. elementName .. "#6464ff.", player, 100, 100, 255, true )
		callClientFunction ( player, "g_GridUpdate" )
	else
		outputChatBox ( "Odczepianie #9696ff" .. elementName .. "#6464ff nie powiodło się!", player, 100, 100, 255, true )
	end
end

function P3visibilityToggle ( player, element, elementName, alpha )
	if setElementAlpha ( element, alpha ) then
		outputChatBox ( "Pomyślnie zmieniono widoczność elementu #9696ff" .. elementName .. "#6464ff.", player, 100, 100, 255, true )
		callClientFunction ( player, "g_GridUpdate" )
	else
		outputChatBox ( "Zmiana widoczności elementu #9696ff" .. elementName .. "#6464ff nie powiodła się!", player, 100, 100, 255, true )
	end
end


function P3collisionToggle ( player, element, elementName, boolean )
	if setElementCollisionsEnabled ( element, boolean ) then
		local boolToText = { [ true ] = "włączono", [ false ] = "wyłączono" }
		outputChatBox ( "Pomyślnie #9696ff" .. boolToText [ boolean ] .. "#6464ff kolizję elementu #9696ff" .. elementName .. "#6464ff.", player, 100, 100, 255, true )
		callClientFunction ( player, "g_GridUpdate" )
	else
		outputChatBox ( "Zmiana kolizji elementu #9696ff" .. elementName .. "#6464ff nie powiodła się!", player, 100, 100, 255, true )
	end
end

function P3scale ( player, element, elementName, factor )
	if setObjectScale ( element, factor ) then
		outputChatBox ( "Pomyślnie zmieniono skalę elementu #9696ff" .. elementName .. "#6464ff.", player, 100, 100, 255, true )
		callClientFunction ( player, "g_GridUpdate" )
	else
		outputChatBox ( "Zmiana skali elementu #9696ff" .. elementName .. "#6464ff nie powiodła się!", player, 100, 100, 255, true )
	end
end